package com.cb.serviceimpl;

import com.alibaba.fastjson.JSON;
import com.cb.pojo.Film;
import com.cb.pojo.ScreeninFormation;
import com.cb.pojo.Screens;
import com.cb.dao.FilmDao;
import com.cb.service.FilmService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class  FilmServiceImpl implements FilmService {

    @Autowired
    FilmDao filmDao;

    List<Film> listall = new ArrayList<>();
    @Override
    public String filmInsert(Film film, HttpServletRequest request) throws SQLException, IOException {

        System.out.println(film.getCountries()+" "+film.getTimelength());
        //String fileName = film.getImages().getOriginalFilename();
        //String headPath = "D:\\xmimages\\" + fileName;
        //film.setImage(headPath);
        //File dest = new File(headPath);
        //film.getImages().transferTo(dest);
        if(filmDao.filmInsert(film)==1){
            return "添加成功";
        }
        else{
            return "添加失败";
        }
    }

    @Override
    public String selectFilm(HashMap json, HttpServletRequest request) {
        String yyid = (String) json.get("yyid");
        listall=filmDao.selectFilm(yyid);
        System.out.println(listall.size());
        String json1 = JSON.toJSONString(listall);
        System.out.println(json1);
        return json1;
        //System.out.println(filmDao.selectFilm("051600192014").getId());
        //return "a";
    }

    @Override
    public String selectFilmother(HashMap json, HttpServletRequest request) {
        String countries,type, markettime;
        countries = (String) json.get("countries");
        type = (String )json.get("type");
        markettime = (String) json.get("markettime");
        List<Film> list = new ArrayList<>();
        int[] num = new int[listall.size()];
        if (countries!=null){
            for (int i=0;i<list.size();i++){
                if(!list.get(i).getCountries().equals(countries)){
                    num[i]=1;
                }
            }
        }
        if(type!=null){
            for (int i=0;i<list.size();i++){
                if(!list.get(i).getType().equals(type)){
                    num[i]=1;
                }
            }
        }
        if(markettime!=null){
            for (int i=0;i<list.size();i++){
                String date = list.get(i).getMarkettime().substring(0,4);
                if(!date.equals(markettime)){
                    num[i]=1;
                }
            }
        }
        for (int i=0;i<listall.size();i++){
            if(num[i]==0){
                list.add(listall.get((i)));
            }
        }
        String json1 = JSON.toJSONString(list);
        return  json1;
    }

    @Override
    public String insertScreeninformation(HashMap json, HttpServletRequest request) throws ParseException {
        ScreeninFormation  screeninFormation = new ScreeninFormation();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //String date = (String) json.get("nowdate");
        //Date nowdate = df.parse(date);
        screeninFormation.setId(Integer.parseInt(String.valueOf(json.get("id"))))
                .setYyid(String.valueOf(json.get("yyid")))
                .setNumberid(Integer.parseInt(String.valueOf(json.get("numberid"))))
                .setYpid(String.valueOf(json.get("ypid")))
                .setBegintime(df.parse(String.valueOf(json.get("begintime"))))
                .setEndtime(df.parse(String.valueOf(json.get("endtime"))));
        int number = filmDao.selectNumber(screeninFormation.getYyid(),screeninFormation.getNumberid());
        int x = Integer.parseInt(String.valueOf((int)Math.sqrt(Double.valueOf(number))));
        int y=x;
        int[][] str = new int[x][y];
        screeninFormation.setList(JSON.toJSONString(str));
        if(filmDao.insertScreeninformation(screeninFormation)==1){
            return "a";
        }
        else{
            return "b";
        }
    }

    @Override
    public String deletefilm(HashMap json, HttpServletRequest request) {
        String id = (String) json.get("id");
        String yyid = (String) json.get("yyid");
        if(filmDao.deletefilm(id,yyid)==1){
            return "a";
        }
        else{
            return "b";
        }
    }

    @Override
    public String yyidSelectsiYyid(HashMap json, HttpServletRequest request) throws ParseException {
        String yyid = String.valueOf(json.get("yyid"));
        String ypid = String.valueOf(json.get("ypid"));
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = df.format(new Date());
        Date nowtime = df.parse(now);
        Film film= filmDao.yyidSelectsiYyid(yyid, yyid);
        String jsons = JSON.toJSONString(film);
        return jsons;
    }
}
